package com.sky.controller.admin;

import com.ejlchina.searcher.SearchResult;
import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import com.sky.web.controller.BaseController;
import com.sky.web.domain.AjaxResult;
import com.sky.web.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@Slf4j
@Api(tags = "员工相关接口")
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController extends BaseController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param dto 用户登陆信息
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登陆")
    public AjaxResult login(@RequestBody EmployeeDTO dto) {
        log.info("员工登录：{}", dto);

        Employee employee = employeeService.login(dto);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        claims.put(JwtClaimsConstant.USERNAME, employee.getUsername());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return AjaxResult.success(employeeLoginVO);
    }

    /**
     * 分页查询员工
     * @param dto 员工信息
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询员工")
    public TableDataInfo list(EmployeeDTO dto) {
        log.info("分页查询员工：{}", dto);
        SearchResult<Employee> result = employeeService.searchPage(dto);
        return getDataTable(result);
    }

    /**
     * 根据id查询员工信息
     * @param id 员工id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询员工信息")
    public AjaxResult getInfo(@PathVariable Long id){
        return AjaxResult.success(employeeService.selectEmployeeById(id));
    }

    /**
     * 添加员工
     * @param dto 员工信息
     * @return
     */
    @PostMapping
    @ApiOperation(value = "添加员工")
    public AjaxResult add(@RequestBody EmployeeDTO dto) {
        log.info("新增员工，{}",dto);
        return toAjax(employeeService.insertEmployee(dto));
    }

    /**
     * 修改员工
     * @param dto 员工信息
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改员工")
    public AjaxResult edit(@RequestBody EmployeeDTO dto) {
        log.info("修改员工，{}",dto);
        return toAjax(employeeService.updateEmployee(dto));
    }
    /**
     * 员工修改密码
     * @param dto 员工信息
     * @return
     */
    @PutMapping("/editPassword")
    @ApiOperation(value = "editPassword")
    public AjaxResult editPassword(@RequestBody PasswordEditDTO dto) {
        log.info("修改员工，{}",dto);
        int status = employeeService.editPassword(dto);
        if (status == 2){
            return AjaxResult.error("旧密码错误");
        }
        return AjaxResult.success();
    }


    /**
     * 删除员工
     * @param ids 员工id
     * @return
     */
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除员工")
    public AjaxResult remove(@PathVariable Long[] ids) {
        log.info("删除员工，{}", Arrays.asList(ids));
        return toAjax(employeeService.deleteEmployeeById(ids));
    }


    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "员工退出登陆")
    public AjaxResult logout() {
        return AjaxResult.success();
    }
}
